package datos;

import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;

import conexion.Conexion;

public class AccionesVenta {

	public static int getMaxNumFactura(int tipoVenta){
		
		
		int maxNumFactura=0;
		try{
			Statement stm=Conexion.conectar().createStatement();
			ResultSet rs=stm.executeQuery("select max(idVenta) from Venta where idTipoVenta="+tipoVenta);
			while(rs.next()){
				maxNumFactura=(rs.getInt("max(idVenta)"));
			}
			return maxNumFactura;
		}catch(Exception e){
			e.printStackTrace();
		}
		return maxNumFactura;
	}
	
	public static void addVenta(Venta v){
		try{
			int idVenta=v.getIdVenta();
			int idTipoVenta=v.getIdTipoVenta();
			int idCliente=v.getIdCliente();
			int idPago=v.getIdTipoPago();
			double total=v.getTotal();
			int cantidadPagos=v.getCantidadPagos();
			Statement stm=Conexion.conectar().createStatement();
			if (cantidadPagos==0){
				stm.executeUpdate("insert into venta values ("+idVenta+","+idTipoVenta+","+idCliente+","+idPago+","+total+","+"now(),"+null+")");
			}else{
			stm.executeUpdate("insert into venta values ("+idVenta+","+idTipoVenta+","+idCliente+","+idPago+","+total+","+"now(),"+cantidadPagos+")");
			}
			}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static ArrayList<Venta> getAll(){
		ArrayList<Venta> ventas=new ArrayList<Venta>();
		try{
			Statement stm=Conexion.conectar().createStatement();
			ResultSet rs=stm.executeQuery("select * from venta");
			while(rs.next()){
				Venta v=new Venta(rs.getInt("idVenta"),rs.getDate("fecha"), rs.getInt("idPago"), rs.getInt("idTipoVenta"), rs.getInt("idCliente"),rs.getDouble("total"));
				v.setCantidadPagos(rs.getInt("cantidadPagos"));
				ventas.add(v);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return ventas;
	}
	
	public static Venta getOne(int idVenta, int idTipoVenta){
		Venta v=null;
		try{
			Statement stm=Conexion.conectar().createStatement();
			ResultSet rs=stm.executeQuery("select * from venta where idVenta="+idVenta+" and idTipoVenta="+idTipoVenta);
			while(rs.next()){
				 v=new Venta(rs.getInt("idVenta"), rs.getDate("fecha"), rs.getInt("idPago"),rs.getInt("idTipoVenta"), rs.getInt("idCliente"), rs.getDouble("total"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return v;
	}
	
	public static ArrayList<Venta> getVentasByCliente(int idCliente){
		ArrayList<Venta> ventas = new ArrayList<Venta>();
		try{
			Statement stm=Conexion.conectar().createStatement();
			ResultSet rs=stm.executeQuery("select * from venta where idCliente="+idCliente);
			while(rs.next()){
				 Venta v = new Venta(rs.getInt("idVenta"), rs.getDate("fecha"), rs.getInt("idPago"),rs.getInt("idTipoVenta"), rs.getInt("idCliente"), rs.getDouble("total"));
				 v.setCantidadPagos(rs.getInt("cantidadPagos"));
				 ventas.add(v);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return ventas;
	}
	
	public static boolean existsClienteVenta(int idCliente){
		try{
			Statement stm=Conexion.conectar().createStatement();
			ResultSet rs=stm.executeQuery("select * from venta where idCliente="+idCliente);
			if(rs.next()){
				return true;
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
}
